<template>
  <div class="app-container">

    <!-- 搜索框 -->
  <FormsTransPay @handleQuery="handleQuery"  @resetQuery=resetQuery :queryParams="queryParams"  :showSearch="showSearch" />

     <!-- 操作按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus" circle
          @click="handleAdd"
          v-hasPermi="['basic:transPay:add']"
        ></el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="refresh-right" circle
          @click="resetQuery"
          v-hasPermi="['customer:customer:list']"
        ></el-button> 
      </el-col>
    
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download" circle
          @click="handleExport"
          v-hasPermi="['basic:transPay:export']"
        ></el-button>
      </el-col>
      
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

   <!-- 表格主体 -->
    <ListTransPayTable @handleUpdate="handleUpdate" @handleDelete="handleDelete" :loading="loading" :transPayList="transPayList" :trans_pay_status="trans_pay_status" />
    
       <!-- 分页功能 -->
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

     <!-- 添加或修改供应商信息对话框 -->
      <Save @submitFormBox="submitForm" @cancelBox="cancel" :title="title" :open="open" :formInfo="form" :trans_pay_status="trans_pay_status"/>
    

  </div>
</template>

<script setup name="TransPay">
import { listTransPay, getTransPay, delTransPay, addTransPay, updateTransPay } from "@/api/basic/transPay";
import FormsTransPay from '@/components/TransPay/FormsTransPay.vue';
import ListTransPayTable from '@/components/TransPay/ListTransPay.vue';
import Save from '@/components/TransPay/SaveTransPay.vue';




const { proxy } = getCurrentInstance();
const { trans_pay_status } = proxy.useDict('trans_pay_status');

const transPayList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    carrier: null,
    startCity: null,
    endCity: null,
  },
  rules: {
    carrier: [
      { required: true, message: "承运商不能为空", trigger: "blur" }
    ],
    startCity: [
      { required: true, message: "始发城市不能为空", trigger: "blur" }
    ],
    endCity: [
      { required: true, message: "到货城市不能为空", trigger: "blur" }
    ],
    kilogramMoney: [
      { required: true, message: "单公斤运费不能为空", trigger: "blur" }
    ],
    cubeMoney: [
      { required: true, message: "立方运费不能为空", trigger: "blur" }
    ],
    minMoney: [
      { required: true, message: "最小运费不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "状态不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询运费列表 */
function getList() {
  loading.value = true;
  listTransPay(queryParams.value).then(response => {
    transPayList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    carrier: null,
    startCity: null,
    endCity: null,
    kilogramMoney: null,
    cubeMoney: null,
    minMoney: null,
    status: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null,
    isDelete: null
  };
  proxy.resetForm("transPayRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryParams.value = {
   pageNum: 1,
   pageSize: 10,
};
handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加运费";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getTransPay(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改运费";
  });
}

/** 提交按钮 */
function submitForm(dataFrom) {
      if (dataFrom.id != null) {
        updateTransPay(dataFrom).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addTransPay(dataFrom).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除运费编号为"' + _ids + '"的数据项？').then(function() {
    return delTransPay(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('basic/transPay/export', {
    ...queryParams.value
  }, `transPay_${new Date().getTime()}.xlsx`)
}


getList();
</script>
